//
//  NSObject+DebugDescription.m
//  ModelPoTest
//
//  Created by mac on 2019/5/3.
//  Copyright © 2019 Yang. All rights reserved.
//

#import "NSObject+DebugDescription.h"
#import <objc/runtime.h>
@implementation NSObject (DebugDescription)


//- (NSString *)description
//{
//    return [self debugDescription];
//
//}

//- (NSString *)debugDescription
//{
//    unsigned int count;
//    const char *clasName = object_getClassName(self);
//    NSMutableString *string = [NSMutableString stringWithFormat:@"<%s: %p>:[ \n",clasName, self];
//    Class clas = NSClassFromString([NSString stringWithCString:clasName encoding:NSUTF8StringEncoding]);
//    Ivar *ivars = class_copyIvarList(clas, &count);
//    for (int i = 0; i < count; i++)
//    {
//        @autoreleasepool{
//            Ivar ivar = ivars[i];
//            const char *name = ivar_getName(ivar);
//            //得到类型
//            NSString *type = [NSString stringWithCString:ivar_getTypeEncoding(ivar) encoding:NSUTF8StringEncoding];
//            NSString *key = [NSString stringWithCString:name encoding:NSUTF8StringEncoding];
//            id value = [self valueForKey:key];
//            //确保BOOL值输出的是YES 或 NO
//            if ([type isEqualToString:@"B"])
//            {
//                value = (value == 0 ? @"NO" : @"YES");
//                
//            }
//            [string appendFormat:@"\t%@:%@\n",[self deleteUnderLine:key], value];
//            
//        }
//        
//    }
//    [string appendFormat:@"]"];
//    return string;
//}
//
///// 去掉下划线
//- (NSString *)deleteUnderLine:(NSString *)string
//{
//    if ([string hasPrefix:@"_"])
//    {
//        return [string substringFromIndex:1];
//        
//    }
//    return string;
//    
//}


- (NSString *)debugDescription {
    //判断是否时NSArray 或者NSDictionary NSNumber 如果是的话直接返回 debugDescription
    if ([self isKindOfClass:[NSArray class]] || [self isKindOfClass:[NSDictionary class]] || [self isKindOfClass:[NSString class]] || [self isKindOfClass:[NSNumber class]]) {
        return [self description];
    }
    //声明一个字典
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
    //得到当前class的所有属性
    uint count;
    objc_property_t *properties = class_copyPropertyList([self class], &count);
    
    //循环并用KVC得到每个属性的值
    for (int i = 0; i<count; i++) {
        objc_property_t property = properties[i];
        NSString *name = @(property_getName(property));
        id value = [self valueForKey:name]?:@"nil";//默认值为nil字符串
        [dictionary setObject:value forKey:name];//装载到字典里
    }
    
    //释放
    free(properties);
    
    //return
    return [NSString stringWithFormat:@"<%@: %p> -- %@",[self class],self,dictionary];
}



//-(NSString *)description
//{
//    return [self debugDescription];
//}
//
//- (NSString *)debugDescription {
//    if ([self isKindOfClass:[NSArray class]] || [self isKindOfClass:[NSDictionary class]] || [self isKindOfClass:[NSNumber class]] || [self isKindOfClass:[NSString class]]) {
//        return self.debugDescription;
//    }
//    //初始化一个字典
//    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
//    //得到当前class的所有属性
//    uint count;
//    objc_property_t *properties = class_copyPropertyList([self class], &count);
//    for (int i = 0; i<count; i++) { //循环并用KVC得到每个属性的值
//        objc_property_t property = properties[i];
//        NSString *name = @(property_getName(property));
//        id value = [self valueForKey:name]?:@"nil";//默认值为nil字符串
//        [dictionary setObject:value forKey:name];//装载到字典里
//    }
//    //释放
//    free(properties);
//    //return
//    return [NSString stringWithFormat:@"<%@: %p> -- %@",[self class],self,dictionary];
//}
@end
